package view;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * Email preview area on the bottom
 * 
 * @author Farzana Alam (farzana22@gmail.com)
 * 
 */
@SuppressWarnings("serial")
public class PreviewView extends JPanel {
	
	// Display fields to be set from external controller
	private JTextField to, from, date, subject;
	
	// Body to be set from external
	private JTextArea body;
	
	// Date format to be displayed
	private String dateFormat;
	
	/**
	 * @param migLayout
	 */
	public PreviewView(MigLayout migLayout) {

		// Calling super constructor
		super(migLayout);
		
		// Set date display format
		dateFormat = "d MMMM, yyyy";
		
		setupComponents();
	}
	
	/**
	 * Setup components in this panel
	 */
	private void setupComponents(){

		// Initialize
		date = new JTextField();
		from = new JTextField();
		to = new JTextField();
		subject = new JTextField();
		body = new JTextArea();
		JScrollPane bodyScroll = new JScrollPane(body);
		
		// Set properties
		date.setEditable(false);
		from.setEditable(false);
		to.setEditable(false);
		subject.setEditable(false);
		body.setEditable(false);
		
		// add
		this.add(new JLabel("From:"));
		this.add(from, "wrap, growx");
		this.add(new JLabel("To:"));
		this.add(to, "wrap, push, growx");
		this.add(new JLabel("Date:"));
		this.add(date, "wrap, growx");
		this.add(new JLabel("Subject:"));
		this.add(subject, "wrap, growx");
		this.add(bodyScroll, "span 2, growx, h 100:400:");
	}

	/**
	 * Sets the To field
	 * @param toText text
	 */
	public void setTo(String toText) {
		this.to.setText(toText);
		;
	}
	
	/**
	 * Sets the From field
	 * @param fromText text
	 */
	public void setFrom(String fromText) {
		this.from.setText(fromText);
	}
	
	/**
	 * Sets the date field
	 * @param timestamp
	 */
	public void setDate(Timestamp timestamp) {
		String d = new SimpleDateFormat(dateFormat).format(timestamp);
		this.date.setText(d);
	}
	
	/**
	 * Sets the subject field
	 * @param subjectText text
	 */
	public void setSubject(String subjectText) {
		this.subject.setText(subjectText);
	}

	/**
	 * Sets the body field
	 * @param bodyText text
	 */
	public void setBody(String bodyText) {
		this.body.setText(bodyText);
	}
	

}
